﻿using ShareCode.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ClientInfrastructure.Model
{
    public class TreeViewModel<T> where T : BaseTreeEntity
    {
        public List<TreeViewModel<T>> Children { get; set; } = [];
        public T? Model { get; set; }


        public static List<TreeViewModel<T>> ChangeListToViewModel(List<T> values)
        {
            List<TreeViewModel<T>> fun(List<T> values, long pid)
            {
                List<TreeViewModel<T>> rere = [];

                var aa = values.Where(t => t.TreeParentId == pid).ToList();
                foreach (var item in aa)
                {
                    TreeViewModel<T> viewModel = new()
                    {
                        Children = fun(values, item.Id),
                        Model = item
                    };

                    rere.Add(viewModel);
                }

                return rere;
            }

            List<TreeViewModel<T>> re = [];

            return fun(values, 0);
        }

    }
}
